package site.wenneng.dev.service;

import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.system.service.ISysDictDataService;
import com.ruoyi.system.service.ISysDictTypeService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import site.wenneng.dev.common.enumeration.DicEnum;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
@AllArgsConstructor
public class DictionaryService {

    private ISysDictTypeService dictTypeService;
    private ISysDictDataService dictDataService;

    public void deleteDictData(DicEnum dicEnum) {
        Long[] ids = listDic(dicEnum).stream().map(SysDictData::getDictCode).toArray(Long[]::new);
        dictDataService.deleteDictDataByIds(ids);
    }

    public void saveSysDictData(SysDictData sysDictData) {
        dictDataService.insertDictData(sysDictData);
    }

    public void updateSysDictData(SysDictData sysDictData) {
        dictDataService.updateDictData(sysDictData);
    }

    public List<SysDictData> listDic(DicEnum dicEnum) {
        return dictTypeService.selectDictDataByType(dicEnum.getValue());
    }

    public Set<String> listValueSet(DicEnum dicEnum) {
        return listDic(dicEnum).stream()
                .map(SysDictData::getDictValue)
                .collect(Collectors.toSet());
    }

    public Set<String> listLabelSet(DicEnum dicEnum) {
        return listDic(dicEnum).stream()
                .map(SysDictData::getDictLabel)
                .collect(Collectors.toSet());
    }
}
